package org.apache.struts.pharmacy.pharmacy.action;

import java.util.Date;
import java.util.Map;

import org.apache.struts.pharmacy.pharmacy.model.HL7Message;
import org.apache.struts.pharmacy.pharmacy.model.ReleaseDrugs;

import com.earogya.communicate.Communicator;
import com.earogya.logic.pharmacy.release.CreateRDSO13;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

/***
 * Class consists with the struts action to release drugs from the pharmacy.
 * @author klanerolle@gmail.com
 *
 */
public class ReleaseDrugsAction extends ActionSupport{
	private ReleaseDrugs releaseDrugs;
	private HL7Message message;
	private Date date;

	public HL7Message getMessage() {
		return message;
	}

	/**
	 * Overrides the execute method is ActionSupport interface,When the action occurs this method is executed.
	 * returns SUCCESS if there are no errors.
	 */
	public String execute() throws Exception {
		Map<String, Object> session=ActionContext.getContext().getSession();
		CreateRDSO13 createMessage=new CreateRDSO13(releaseDrugs);
		String msg=createMessage.createMessage((String)session.get("facility"));
		message=new HL7Message();
		if(!msg.equals(null)){
			Communicator c=new Communicator();
			String reply=c.sendMessage(msg);
			message.setMessage(msg+"-------------"+reply);
		}
		else{
			message.setMessage("empty");
		}
		return SUCCESS;
	}

	public ReleaseDrugs getReleaseDrugs() {
		return releaseDrugs;
	}

	public void setReleaseDrugs(ReleaseDrugs releaseDrugs) {
		this.releaseDrugs = releaseDrugs;
	}

	/**
	 * Overrides the validate method in ActionSupport interface.
	 * Validates the input fields for correct input.
	 */
	public void validate(){
		if(releaseDrugs.getPatientId().length()== 0){
			addFieldError( "releaseDrugs.patientId", "Patient ID required" );
		}else if(!this.isInteger(releaseDrugs.getPatientId())){
			addFieldError( "releaseDrugs.patientId", "Incorrect patient ID format." );
		}
		
		if(releaseDrugs.getReleaseId().length()== 0){
			addFieldError( "releaseDrugs.releaseId", "Release ID required" );
		}else if(!this.isInteger(releaseDrugs.getReleaseId())){
			addFieldError( "releaseDrugs.releaseId", "Incorrect Release ID format." );
		}
		
		if(releaseDrugs.getDrugId().length()== 0){
			addFieldError( "releaseDrugs.drugId", "Drug ID required" );
		}
		
		if(releaseDrugs.getQuantity().length()== 0){
			addFieldError( "releaseDrugs.quantity", "Quantity required" );
		}else if(!this.isInteger(releaseDrugs.getQuantity())){
			addFieldError( "releaseDrugs.quantity", "Incorrect Quantity format." );
		}
		
		if(releaseDrugs.getUnits().length()== 0){
			addFieldError( "releaseDrugs.units", "Unit required" );
		}
		
		if(releaseDrugs.getUnitOrWard().length()== 0){
			addFieldError( "releaseDrugs.unitOrWard", "Ward required" );
		}
		
		if(releaseDrugs.getReleaseDate()== null){
			addFieldError( "releaseDrugs.releaseDate", "Release Date required" );
		}
	}
	
	/**
	 * Check if the given string is an integer.
	 * @param value
	 * @return true is the value is an integer
	 */
	private Boolean isInteger(String value){
		Boolean isInteger=false;
		try{
			int intValue=Integer.parseInt(value);
			isInteger=true;
		}catch(NumberFormatException e){
			isInteger=false;
		}
		return isInteger;
	}
}
